home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / TIMING.SWG / 0014_Timing Unit.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  5KB  |  209 lines

  1. {
  2. > Now what I want to do is calculate the total run-time of the overall
  3. > event, from start to finish, i.e., parse the log file taking the last and
  4. > first time entries and calculate the time. I'm sure there is an easier way
  5. > to do this but I'm new to Pascal, and, open to suggestions.  Below is what
  6. > appears in the event.log :
  7. }
  8.  
  9. Unit Timer;
  10.  
  11. {       SIMPLE TIMER 1.0
  12.         =================
  13.  
  14.  This is a Timer unit, it calculates time by system clock.  A few limitations
  15.  are:
  16.  
  17.    1) Must not modify clock.
  18.    2) Must not time more than a day
  19.    3) Must StopTimer before displaying Time
  20.  
  21.    Usage:
  22.  
  23.       StartTimer;   Starts Timer
  24.       StopTimer;    Stops Timer
  25.       CalcTimer;    Calculates time
  26.       DispTime:     Displays time between StartTimer and StopTimer,
  27.                     you don't need to call CalcTimer if you call DispTime.
  28.  
  29.  This unit may be used in freeware and shareware programs as long as:
  30.  
  31.    1) The program is a DECENT program, no "Adult" or "XXX" type programs
  32.       shall lawfully contain any code found within this file (modified or
  33.       in original form) or this file after it's been compiled.
  34.  
  35.    2) This copyrighting is not added to, or removed from the program by
  36.       any other person other than I, the author.
  37.  
  38.  This is copyrighted but may be used or modified in programs as long as the
  39.  above conditions are followed.
  40.  
  41.  I may be reached at:
  42.  
  43.    1:130/709                              - Fidonet
  44.    Chris.Boyd@f709.n130.z1.fidonet.org    - Internet
  45.    Alpha Zeta, Ft. Worth (817) 246-3058   - Bulletin Board
  46.  
  47.  If you have any comments or suggestions (not complaints).  I assume no
  48.  responsibility for anything resulting from the usage of this code.
  49.  
  50.                                                    -Chris Boyd
  51.  
  52. }
  53.  
  54. Interface
  55.  
  56. Uses
  57.   Dos;
  58.  
  59. Type
  60.   TimeStruct = record
  61.     Hour,
  62.     Minute,
  63.     Second,
  64.     S100   : Word;
  65.   End;
  66.  
  67. Var
  68.   StartT,
  69.   StopT,
  70.   TimeT   : TimeStruct;
  71.   Stopped : Boolean;
  72.  
  73. procedure StartTimer;
  74. procedure StopTimer;
  75. procedure DispTime;
  76. procedure CalcTimer;
  77.  
  78. Implementation
  79.  
  80. procedure TimerError(Err : Byte);
  81. Begin
  82.   Case Err of
  83.     1 :
  84.     Begin
  85.       Writeln(' Error: Must Use StartTimer before StopTimer');
  86.       Halt(1);
  87.     End;
  88.  
  89.     2 :
  90.     Begin
  91.       Writeln(' Error: Timer can not handle change of day');
  92.       Halt(2);
  93.     End;
  94.  
  95.     3 :
  96.     Begin
  97.       Writeln(' Error: Internal - Must StopTimer before DispTime');
  98.       Halt(3);
  99.     End;
  100.   End;
  101. End;
  102.  
  103. procedure CalcTimer;
  104. Begin
  105.   If (Stopped = True) Then
  106.   Begin
  107.     If (StopT.Hour < StartT.Hour) Then
  108.       TimerError(2);
  109.     TimeT.Hour := StopT.Hour - StartT.Hour;
  110.  
  111.     If (StopT.Minute < StartT.Minute) Then
  112.     Begin
  113.       TimeT.Hour   := TimeT.Hour - 1;
  114.       StopT.Minute := StopT.Minute + 60;
  115.     End;
  116.     TimeT.Minute := StopT.Minute - StartT.Minute;
  117.  
  118.     If (StopT.Second < StartT.Second) Then
  119.     Begin
  120.       TimeT.Minute := TimeT.Minute - 1;
  121.       StopT.Second := StopT.Second + 60;
  122.     End;
  123.     TimeT.Second := StopT.Second - StartT.Second;
  124.  
  125.     If (StopT.S100 < StartT.S100) Then
  126.     Begin
  127.       TimeT.Second := TimeT.Second - 1;
  128.       StopT.S100   := StopT.S100 + 100;
  129.     End;
  130.     TimeT.S100 := StopT.S100 - StartT.S100;
  131.   End
  132.   Else
  133.     TimerError(3);
  134. End;
  135.  
  136. procedure DispTime;
  137. Begin
  138.   CalcTimer;
  139.   Write(' Time : ');
  140.   Write(TimeT.Hour);
  141.   Write(':');
  142.  
  143.   If (TimeT.Minute < 10) Then
  144.     Write('0');
  145.   Write(TimeT.Minute);
  146.   Write(':');
  147.  
  148.   If (TimeT.Second < 10) Then
  149.     Write('0');
  150.   Write(TimeT.Second);
  151.   Write('.');
  152.  
  153.   If (TimeT.S100 < 10) Then
  154.     Write('0');
  155.   Writeln(TimeT.S100);
  156. End;
  157.  
  158. procedure StartTimer;
  159. Begin
  160.   GetTime(StartT.Hour, StartT.Minute, StartT.Second, StartT.S100);
  161.   Stopped := False;
  162. End;
  163.  
  164. procedure StopTimer;
  165. Begin
  166.   If (Stopped = False) Then
  167.   Begin
  168.     GetTime(StopT.Hour, StopT.Minute, StopT.Second, StopT.S100);
  169.     Stopped := TRUE;
  170.   End
  171.   Else
  172.     TimerError(1);
  173. End;
  174.  
  175. End.
  176.  
  177. {
  178. This is a unit that I wrote.  It will not change day without calling an error
  179. in itself.  This can be modified though, I just haven't went about doing it.
  180. For example, if you started the timer at 11:29 pm and stopped it at 1:00 am, it
  181. wouldn't work, but if you started the timer at 12:00 am and stopped it at 11:59
  182. pm in that same day it would work.  The TimeStruct type doesn't store day, just
  183. time and the only thing you have to do to use it is:
  184.  
  185. In your main program:
  186. }
  187. Program MyProg;
  188.  
  189. Uses
  190.   Timer;
  191.  
  192. Begin
  193. { Program stuff.... }
  194. StartTimer;
  195. { More Program Stuff... }
  196. StopTimer;
  197. { If you don't want to display the time to the screen, then you need to
  198.   call CalcTimer, so that it modifies TimeT}
  199. DispTime; {Whenever you want to display the time..  The calculated time is
  200. stored in the record variable Timer.TimeT, if you wanted to access    it.  All
  201. the fields of the record a word in type.  To access the hours for example,
  202. you'd go like:
  203.  
  204.                 Timer.TimeT.Hour    or    TimeT.Hour
  205.  
  206.            You probably will have to try both.}
  207. End.
  208.  
  209.